package com.ymatou.shop.ui.activity.seller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ymatou.shop.Constants;
import com.ymatou.shop.R;
import com.ymatou.shop.api.ApiRequest;
import com.ymatou.shop.api.ApiResponse;
import com.ymatou.shop.cache.ImageCache;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.cache.exception.CacheException;
import com.ymatou.shop.model.LeaveMessage;
import com.ymatou.shop.model.Product;
import com.ymatou.shop.ui.activity.BaseHeaderListActivity;
import com.ymatou.shop.ui.adapter.BaseAdapter;
import com.ymatou.shop.ui.view.PullToRefreshView;
import com.ymatou.shop.ui.view.item.LeaveMessageItemView;
import com.ymatou.shop.util.DateUtil;
import com.ymatou.shop.util.StringUtil;

public class OrderDetailActivity extends BaseHeaderListActivity {
	
	private static final int REQUEST_ID_LIST = 0x01;
	
	private ImageView mProductImageView;
	
	private LeaveMessageAdapter mAdapter;
	private List<LeaveMessage> mLeaveMessageList;
	private Product mProduct;
	
	private String mLastId;
	private volatile boolean mReachEnd;
	private volatile boolean mLoadingList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_detail);
		
		mProduct = (Product) getIntent().getSerializableExtra(Constants.EXTRA_DATA);
		if (mProduct == null) {
			finish();
			return;
		}
		
		mLeaveMessageList = new ArrayList<LeaveMessage>();
		mAdapter = new LeaveMessageAdapter(this, mLeaveMessageList);
		
		setupViews();
		setupData(false);
	}
	
	@Override
	protected void setupViews() {
		super.setupViews();
		getTitleTextView().setText(R.string.title_order_detail);
		getTitleLeftImageButton().setImageResource(R.drawable.back_icon);
		getTitleLeftImageButton().setVisibility(View.VISIBLE);
		getTitleRightImageButton1().setImageResource(R.drawable.refresh_icon);
		getTitleRightImageButton1().setVisibility(View.VISIBLE);
		getTitleRightImageButton1().setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getPullToRefreshView().updateWithoutOffset();
				getPullToRefreshView().scrollToUpdate();
				setupData(false);
			}
		});
		getPullToRefreshView().setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				mLastId = null;
				setupData(false);
			}
		});
		
		
		View headerView = getLayoutInflater().inflate(R.layout.include_seller_order_detail_header, getListView(), false);
		getListView().addHeaderView(headerView);
		getListView().setAdapter(mAdapter);
		
		
		((TextView) headerView.findViewById(R.id.description_txt))
			.setText(StringUtil.abbreviate(mProduct.mDescription, 45));
		((TextView) headerView.findViewById(R.id.time_txt))
			.setText(DateUtil.format(new Date(), "MM-dd HH:mm:ss"));/////////////////
		((TextView) headerView.findViewById(R.id.downpayment_txt))
			.setText(String.valueOf(mProduct.mDownPayment));
		((TextView) headerView.findViewById(R.id.price_txt))
			.setText(String.valueOf(mProduct.mPrice));
		((TextView) headerView.findViewById(R.id.stock_num_txt))
			.setText(String.valueOf(mProduct.mStockNum));
		((TextView) headerView.findViewById(R.id.sale_num_txt))
			.setText(String.valueOf(mProduct.mSaleNum));
		
		mProductImageView = (ImageView) headerView.findViewById(R.id.icon_image);
		Drawable drawable = ImageCache.get(this, mProduct.mHomePic, new ImageCache.OnIconLoadCompleteListener() {
			@Override
			public void onLoadComplete(String url, boolean success, Drawable drawable) {
				if (success) {
					mProductImageView.setImageDrawable(drawable);
				}
			}
		});
		if (drawable != null) {
			mProductImageView.setImageDrawable(drawable);
		} else {
			mProductImageView.setImageResource(R.drawable.ic_launcher);
		}
		
		changeLoadingViewState(false);
	}
	
	@Override
	protected void setupData(boolean useCache) {
		if (!mReachEnd) {
			if (!mLoadingList) {
				RequestInfo requestInfo = ApiRequest.getSellerProductLeaveMessageList(mProduct.mId, mProduct.mSellerId, useCache);
				getCacheManager().register(REQUEST_ID_LIST, requestInfo, this);
			}
		}
	}
	
	@Override
	public BaseAdapter getAdapter() {
		return mAdapter;
	}
	
	@Override
	public void onCacheSuccess(int requestId, RequestInfo requestInfo,
			Object response) {
		if (requestId == REQUEST_ID_LIST) {
//			//////////test///
			if (mLeaveMessageList.size() == 0) {
				LeaveMessage m = new LeaveMessage();
				m.mMessage = "测试留言测试留言测试留言-结束";
				m.mBuyerName = "测试名称";
				
				mLeaveMessageList.add(m);mLeaveMessageList.add(m);mLeaveMessageList.add(m);mLeaveMessageList.add(m);mLeaveMessageList.add(m);
				mLeaveMessageList.add(m);mLeaveMessageList.add(m);mLeaveMessageList.add(m);mLeaveMessageList.add(m);mLeaveMessageList.add(m);
				
				changeLoadingViewState(true);
			}
			
			if (mLeaveMessageList.size() > 0) {
				getPullToRefreshView().onRefreshComplete(new Date());
			} else {
				getPullToRefreshView().setLastUpdated(new Date());
			}
			return;
			/////////////
//			List<LeaveMessage> list = ApiResponse.getProductLeaveMessageList((String) response);
//			if (list == null) {
//				if (mLeaveMessageList.size() == 0) {
//					changeErrorViewState(false);
//				}
//				return;
//			}
//			
//			if (mLeaveMessageList.size() == 0) {
//				mLeaveMessageList.addAll(list);
//				
//				mAdapter = new LeaveMessageAdapter(this, mLeaveMessageList);
//				getListView().setAdapter(mAdapter);
//				getPullToRefreshView().setLastUpdated(new Date());
//				
//				changeLoadingViewState(true);
//			} else {
//				if (getPullToRefreshView().isRefresh()) {
//					mLeaveMessageList.clear();
//				}
//				mLeaveMessageList.addAll(list);
//				mAdapter.notifyDataSetChanged();
//				getPullToRefreshView().onRefreshComplete(new Date());
//			}
//			
//			if (list.isEmpty()) {
//				mReachEnd = true;
//			} else {
////				mLastId = list.get(list.size() - 1).mId;
//			}
//			mLoadingList = false;
		} else {
			super.onCacheSuccess(requestId, requestInfo, response);
		}
	}
	
	@Override
	public void onCacheFailed(int requestId, RequestInfo requestInfo,
			CacheException error) {
		if (requestId == REQUEST_ID_LIST) {
			changeErrorViewState(false);
		} else {
			super.onCacheFailed(requestId, requestInfo, error);
		}
	}
	
	private class LeaveMessageAdapter extends BaseAdapter<LeaveMessage> {

		public LeaveMessageAdapter(Context context, List<LeaveMessage> items) {
			super(context, items);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LeaveMessageItemView itemView;
			if (convertView == null) {
				itemView = new LeaveMessageItemView(getContext());
			} else {
				itemView = (LeaveMessageItemView) convertView;
			}
			
			LeaveMessage item = getItem(position);
			itemView.setData(item);
			
			return itemView;
		}
	}
}
